package com.ht.pms.core.controller.template;

import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ht.pms.core.dto.PageResult;
import com.ht.pms.core.dto.Result;
import com.ht.pms.core.dto.dict.DictParamsDto;
import com.ht.pms.core.dto.tempalte.Template;
import com.ht.pms.core.dto.tempalte.TemplateDetailDto;
import com.ht.pms.core.dto.tempalte.TemplateDetailParam;
import com.ht.pms.core.dto.tempalte.TemplateDetailReq;
import com.ht.pms.core.dto.tempalte.TemplateDetailSortReq;
import com.ht.pms.core.dto.tempalte.TemplateReq;
import com.ht.pms.core.service.SalaryExcelDataService;
import com.ht.pms.core.service.SalaryTemplateService;

/**
 * 模板管理
 * 
 * @author yuewen
 *
 */
//@CrossOrigin
@RestController
@RequestMapping("/salaryTemplate")
public class SalaryTemplateController {
	private static final Logger LOGGER = LoggerFactory.getLogger(SalaryTemplateController.class);

	@Autowired
	private SalaryTemplateService salaryTemplateService;

	/**
	 * 获取模板列表
	 * 
	 * @param tableName
	 * @return
	 */
	@RequestMapping("/getTemplateList")
	public PageResult<List<Template>> getTemplateList(TemplateReq req) {
		LOGGER.info("[薪资模板] 获取模板列表  params:[{}]", req);
		List<Template> templateList = salaryTemplateService.getTemplateList(req);
		int templateCount = salaryTemplateService.getTemplateCount(req);

		return PageResult.success(templateList, templateCount);
	}
	
	/**
	 * 获取导入数据类型模板
	 * 
	 * @param tableName
	 * @return
	 */
	@RequestMapping("/getTemplateListByType")
	public Result<List<Template>> getTemplateListByType() {
		LOGGER.info("[薪资模板] 获取导入数据类型模板");
		List<Template> templateList = salaryTemplateService.getTemplateListByType();
		
		return Result.buildSuccess(templateList);
	}
	
	
	/**
	 * 获取模板字段详情列表
	 * 
	 * @param tableName
	 * @return
	 */
	@RequestMapping("/getTemplateDetailList")
	public PageResult<List<TemplateDetailDto>> getTemplateDetailList(@RequestParam("templateCode") String templateCode) {
		LOGGER.info("[薪资模板] 获取模板字段详情列表  templateCode:[{}]", templateCode);
		List<TemplateDetailDto> templateDetails = salaryTemplateService.getTemplateDetailByTemplateCode(templateCode);
		
		return PageResult.success(templateDetails, templateDetails == null?0:templateDetails.size());
	}
	
	/**
	 * 获取模板字段详情
	 * 
	 * @param tableName
	 * @return
	 */
	@RequestMapping("/getTemplateDetail")
	public Result<Template> getTemplateDetail(String templateCode) {
		LOGGER.info("[薪资模板] 获取模板详情  templateCode:[{}]", templateCode);
		Template template = salaryTemplateService.getTemplateByTemplateCode(templateCode);
		
		return Result.buildSuccess(template);
	}
	
	/**
	 * 保存模板
	 * 
	 * @param tableName
	 * @return
	 */
	@RequestMapping("/saveTemplate")
	public Result saveTemplate(@RequestBody TemplateDetailParam req ) {
		LOGGER.info("[薪资模板] 获取模板详情  req:[{}]", req);
		
		salaryTemplateService.saveTemplate(req);
		
		return Result.buildSuccess();
	}
	
	
	/**
	 * 所有字段列表
	 * 
	 * @return  所有字段列表
	 */
	@RequestMapping("/getAllDictParams")
	public Result<List<DictParamsDto>> getAllDictParams() {
		LOGGER.info("[薪资模板] 所有字段接口");
		List<DictParamsDto> paramList = salaryTemplateService.SalaryBusinessDictList();
		return Result.buildSuccess(paramList);
	}
	
	
	/**
	 * 保存选择的字段
	 * @return
	 */
	@RequestMapping("/saveTemplateDictParams")
	public Result saveTemplateDictParams(HttpServletRequest request, @RequestBody TemplateDetailReq req) {
		LOGGER.info("[薪资模板] 保存选择的字段 req:[{}]", req);
		String templateCode = salaryTemplateService.saveTemplateDictParams(req);
		
		return  Result.buildSuccess(templateCode);
	}
	
	
	/**
	 * 字段排序
	 * @return
	 */
	@RequestMapping("/sortTemplateDictParams")
	public Result sortTemplateDictParams(HttpServletRequest request, @RequestBody TemplateDetailSortReq req) {
		LOGGER.info("[薪资模板] 字段排序req:[{}]", req);
		salaryTemplateService.sortTemplateDictParams(req);
		
		return  Result.buildSuccess();
	}
	
	
	
	
	@GetMapping("/downloadTemplate")
	public Result downloadSalaryFile(@RequestParam("templateCode") String templateCode,
			@RequestParam("templateName") String templateName, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		LOGGER.info("[模板管理]模板下载 ，  param:[{}]", templateCode);
		SXSSFWorkbook book = null;
		OutputStream output = null;
		BufferedOutputStream bufferedOutPut = null;
		book = salaryTemplateService.downloadTemplate(templateCode);
		if (book == null) {
			return Result.buildFail();
		}
		// 指定下载的文件名
		response.setHeader("Content-Disposition",
				"attachment;filename=" + new String(templateName.getBytes("UTF-8"), "ISO-8859-1") + ".xlsx");
		response.setContentType("application/vnd.ms-excel;charset=UTF-8");
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);

		output = response.getOutputStream();
		bufferedOutPut = new BufferedOutputStream(output);
		bufferedOutPut.flush();
		book.write(bufferedOutPut);
		bufferedOutPut.close();

		return Result.buildSuccess(null);
	}
}
